package com.dayuanit.dy17.atm.atmbackoffice.shiro;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.spring.web.config.DefaultShiroFilterChainDefinition;
import org.apache.shiro.spring.web.config.ShiroFilterChainDefinition;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.apache.shiro.mgt.SecurityManager;

import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {

    @Bean
    public AtmRealm atmRealm() {
        AtmRealm atmRealm = new AtmRealm();
        return atmRealm;
    }

    @Bean
    public DefaultWebSecurityManager defaultWebSecurityManager(AtmRealm atmRealm) {
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
        defaultWebSecurityManager.setRealm(atmRealm);
        return defaultWebSecurityManager;
    }

    @Bean
    public DefaultShiroFilterChainDefinition shiroFilterChainDefinition () {
        DefaultShiroFilterChainDefinition filterChainDefinition = new DefaultShiroFilterChainDefinition();

        filterChainDefinition.addPathDefinition("/js/**", "anon");
        filterChainDefinition.addPathDefinition("/css/**", "anon");
        filterChainDefinition.addPathDefinition("/fonts/**", "anon");
        filterChainDefinition.addPathDefinition("/imgages/**", "anon");

        filterChainDefinition.addPathDefinition("/page/toLogin", "anon");
        filterChainDefinition.addPathDefinition("/user/login", "anon");
        filterChainDefinition.addPathDefinition("/page/toUnauthorized", "anon");

        filterChainDefinition.addPathDefinition("/**", "authc");

        return filterChainDefinition;
    }

    //配置ShiroDialect:用于thymeleaf和shiro标签配合使用
    @Bean
    public ShiroDialect getShiroDialect(){
        return new ShiroDialect();
    }
}
